package com.ruoyi.web.controller.medicine;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.medicine.domain.HisMedicineDrug;
import com.ruoyi.medicine.service.iHisMedicineCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: RuoYi-Vue-master
 * @ClassName HisMedicineCartServiceController
 * @description:
 * @author: xiaoJie
 * @create: 2024-09-01 15:19
 * @Version 1.0
 */
@RestController
@RequestMapping("/medicine/cart")
public class HisMedicineCartController extends BaseController {

    @Autowired
    private iHisMedicineCartService hisMedicineCartService;

    //修改数量
    @PutMapping("/{drugId}/{num}")
    @PreAuthorize("@ss.hasPermi('medicine:cart:edit')")
    @Log(title = "购物车管理", businessType = BusinessType.UPDATE)
    public  AjaxResult updateCarNum(@PathVariable("drugId") Long drugId,@PathVariable("num") Integer num) throws JsonProcessingException {
        System.out.println(num+drugId);
       return toAjax(hisMedicineCartService.update(getUserId(),drugId,num));
    }
    //删除购物车药品
    @PreAuthorize("@ss.hasPermi('medicine:cart:delete')")
    @Log(title = "购物车删除", businessType = BusinessType.DELETE)
    @DeleteMapping("/{drugId}")
    public AjaxResult deleteCarByDrugId(@PathVariable Long[] drugId) throws JsonProcessingException {
       return toAjax(hisMedicineCartService.deleteMedicineCartByDrugId(drugId,getUserId()));
    }

    //查询所有购物车数据
    @GetMapping("/list")
    @PreAuthorize("@ss.hasPermi('medicine:cart:list')")
    public AjaxResult selectCarListById() throws JsonProcessingException {
        List<HisMedicineDrug> list = hisMedicineCartService.selectCarByUserId(getUserId());
        return AjaxResult.success("成功",list);
    }


    @PreAuthorize("@ss.hasPermi('medicine:cart:add')")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @RequestMapping("/{drugIDs}")
    public AjaxResult add(@PathVariable Long[] drugIDs) throws JsonProcessingException {
        Long userId = getUserId();
        return toAjax(hisMedicineCartService.saveCat(userId,drugIDs));
    }
}
